//
//  AppDelegate.swift
//  0319iOSweiboDemo
//
//  Created by Tang on 16/3/19.
//  Copyright © 2016年 大天朝. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        //设置导航栏皮肤
        setAppearance()
        
        
        //TODO: 测试加载账号
      let userAccount = SLUserAccountViewModel.sharedInstance.userAccount
        print("AppDelegate userAccount: \(userAccount)")
        
        window = UIWindow(frame: UIScreen .mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        window?.rootViewController = defultVuewController()
        window?.makeKeyAndVisible()
        
        return true
    }
    //界面切换.不需要介入,直接可以判断
    private func defultVuewController() -> UIViewController {
        //1.判断是否是登录
        if !SLUserAccountViewModel.sharedInstance.userLogin {
            //没有登录
            return SLMainViewController()
        }
        
        //2.登录过了
       return isNewVersion() ? SLNewfFeatureViewController() : SLWelocomeViewController()
    }
    
    //界面切换统一调用方法
    //外面使用需要拿到对象再调用比较麻烦
    func switchRootViewController(controller: UIViewController) {
        window?.rootViewController = controller
    }
    //外部调用的切换根控制器
    class func outSwitchRootViewController(controller: UIViewController) {
        let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
        appDelegate?.switchRootViewController(controller)
    }
    
    //是否是最新版本
    private func isNewVersion() -> Bool {
        //1.获取当前版本
       let version = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
//        print("version: \(version)")
        
        //2.获取上一个版本
        let sandboxKey = "sandboxKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandboxKey)
//        print("sandboxVersion: \(sandboxVersion)")
        //3.对比
        let newVersion = version != sandboxVersion
        
        //4.是新版本.保存当前版本号
        if newVersion {
            NSUserDefaults.standardUserDefaults().setObject(version, forKey: sandboxKey)
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        
        //5.返回比较结果
        return newVersion
        
    }
    
    private func setAppearance() {
        //设置导航栏皮肤
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        
    }
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

